package com.xzq.bustack.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/20 10:36
 * @Version 1.0.0
 **/
public class MyInMsgHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        SocketChannel sc = (SocketChannel) ctx.channel();
        System.out.println("连接建立....");
        System.out.println("连接IP：" + sc.localAddress().getHostString());
        System.out.println("连接端口：" + sc.localAddress().getPort());

        String str = "咱俩连接建立成功了。。。我是Server";
        ctx.writeAndFlush(str);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("连接断开.....");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("收到客户端消息：" + msg);
        ctx.writeAndFlush("收到消息！");
    }
}
